package com.utils;

import app.LocatorApp;
import com.esotericsoftware.yamlbeans.YamlException;
import com.esotericsoftware.yamlbeans.YamlReader;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhengshuehng on 2016/12/21 0021.
 */
public class YamlReadUtil {
    public static void main(String args[]) throws FileNotFoundException, YamlException {
        String path2 = "src/main/java/org/webdriver/patatiumappui/pageObjectConfig/UiLibrary.yaml";
        YamlReadUtil yamlReadUtil = new YamlReadUtil();
        yamlReadUtil.getLocatorMap(path2, "org.webdriver.patatiumappui.pageObject.PageElenmentApp");
        yamlReadUtil.getYamlPageUrl(path2, "org.webdriver.patatiumappui.pageObject.PageElenmentApp");
    }

    /**
     * @param path     对象库文件地址
     * @param pageName pageName 页面名字
     * @return 返回locator 哈希表  locatorName:locator
     */
    public HashMap<String, LocatorApp> getLocatorMap(String path, String pageName) throws FileNotFoundException, YamlException {
        HashMap<String, LocatorApp> locatorHashMap = new HashMap();
        YamlReader yamlReader = new YamlReader(new FileReader(path));
        Object yamlObject = yamlReader.read();
        Map yamlMap = (Map) yamlObject;
        ArrayList<HashMap<String, Object>> pages = (ArrayList<HashMap<String, Object>>) yamlMap.get("pages");
        for (int i = 0; i < pages.size(); i++)//遍历Page节点
        {
            HashMap<String, Object> pageNode = pages.get(i);//获取page节点
            HashMap<String, Object> pageElement = (HashMap<String, Object>) pageNode.get("page");
            if (pageElement.get("pageName").toString().equalsIgnoreCase(pageName))//判断是否需要获取的Page节点
            {
//                System.out.println(pageElement.get("desc"));
                List<HashMap<String, Object>> locators = (List<HashMap<String, Object>>) pageElement.get("locators");//获取locators列表
                for (int j = 0; j < locators.size(); j++)//遍历locators列表
                {
                    HashMap<String, Object> locatorNode = locators.get(j);
                    LocatorApp locatorApp = new LocatorApp();
                    locatorApp.setType(getByType(locatorNode.get("type").toString()));
                    locatorApp.setValue(locatorNode.get("value").toString());
                    locatorApp.setTimout(Integer.parseInt(locatorNode.get("timeout").toString()));
                    locatorApp.setLocatorName(locatorNode.get("name").toString());
                    locatorHashMap.put(locatorNode.get("name").toString(), locatorApp);
                }

            } else {
                continue;
            }
//            System.out.println(pageObjet);
        }
//        System.out.println(locatorHashMap.get("登录").getLocalorName());
        return locatorHashMap;


    }

    public String getYamlPageUrl(String path, String pageName) throws FileNotFoundException, YamlException {
        Map<String, LocatorApp> locatorHashMap = new HashMap();
        YamlReader yamlReader = new YamlReader(new FileReader(path));
        Object yamlObject = yamlReader.read();
        Map yamlMap = (Map) yamlObject;
        ArrayList<HashMap<String, Object>> pages = (ArrayList<HashMap<String, Object>>) yamlMap.get("pages");
        String url = "";
        for (int i = 0; i < pages.size(); i++)//遍历Page节点
        {
            HashMap<String, Object> pageNode = pages.get(i);//获取page节点
            HashMap<String, Object> pageElement = (HashMap<String, Object>) pageNode.get("page");
            if (pageElement.get("pageName").toString().equalsIgnoreCase(pageName))//判断是否需要获取的Page节点
            {
//                System.out.println(pageElement.get("desc"));
                url = pageElement.get("value").toString();
            } else {
                continue;
            }
//            System.out.println(pageObjet);
        }
        System.out.println(url);
        return url;
    }

    /**
     * @param type
     */
    public static ByType getByType(String type) {
        ByType byType = ByType.xpath;
        if (type == null || type.equalsIgnoreCase("xpath")) {
            byType = ByType.xpath;
        } else if (type.equalsIgnoreCase("id")) {
            byType = ByType.id;
        } else if (type.equalsIgnoreCase("linkText")) {
            byType = ByType.linkText;
        } else if (type.equalsIgnoreCase("name")) {
            byType = ByType.name;
        } else if (type.equalsIgnoreCase("className")) {
            byType = ByType.className;
        } else if (type.equalsIgnoreCase("cssSelector")) {
            byType = ByType.cssSelector;
        } else if (type.equalsIgnoreCase("partialLinkText")) {
            byType = ByType.partialLinkText;
        } else if (type.equalsIgnoreCase("tagName")) {
            byType = ByType.tagName;
        }
        return byType;
    }
}
